package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    private RoomLabelService roomLabelService;

    @Autowired
    private RoomFacilityService  roomFacilityService;

    @Autowired
    private RoomAttrValueService roomAttrValueService;

    @Autowired
    private ApartmentInfoMapper  apartmentInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private AttrValueMapper attrValueMapper;

    @Override
    public List<RoomInfo> getRoomInfoListByApartmentId(Long apartmentId) {
        List<RoomInfo> roomInfoList = baseMapper.selectRoomInfoListByApartmentId(apartmentId);
        return roomInfoList;
    }

    @Override
    @Transactional
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        //有两种情况：保存或更新
        //先保存基本信息
        Long roomId = roomSubmitVo.getId();

        if(roomId != null){
            //更新
            //先删除旧数据，再插入新数据

            //删除相关图片
            QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
            graphInfoQueryWrapper.eq("item_type", ItemType.ROOM);
            graphInfoQueryWrapper.eq("item_id", roomId);
            graphInfoService.remove(graphInfoQueryWrapper);

            //删除相关租期
            QueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new QueryWrapper<>();
            roomLeaseTermQueryWrapper.eq("room_id", roomId);
            roomLeaseTermService.remove(roomLeaseTermQueryWrapper);

            //删除相关支付方式
            QueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new QueryWrapper<>();
            roomPaymentTypeQueryWrapper.eq("room_id", roomId);
            roomPaymentTypeService.remove(roomPaymentTypeQueryWrapper);

            //删除相关房间标签
            QueryWrapper<RoomLabel> roomLabelQueryWrapper = new QueryWrapper<>();
            roomLabelQueryWrapper.eq("room_id", roomId);
            roomLabelService.remove(roomLabelQueryWrapper);

            //删除相关房间配套
            QueryWrapper<RoomFacility> roomFacilityQueryWrapper = new QueryWrapper<>();
            roomFacilityQueryWrapper.eq("room_id", roomId);
            roomFacilityService.remove(roomFacilityQueryWrapper);

            //删除相关房价属性
            QueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper = new QueryWrapper<>();
            roomAttrValueQueryWrapper.eq("room_id", roomId);
            roomAttrValueService.remove(roomAttrValueQueryWrapper);

        }
        //新增或者更新集中一起操作
        //先更新或新增新的房间基本信息
        this.saveOrUpdate(roomSubmitVo);
        Long insertRoomId = roomSubmitVo.getId();

        //新增或更新房间相关属性
        //先得到房间相关的属性的id列表，再根据id封装实体类对象
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        List<RoomAttrValue> roomAttrValueList = attrValueIds.stream()
                .map(attrValueId -> { return RoomAttrValue.builder()
                .roomId(insertRoomId)
                .attrValueId(attrValueId)
                .build();}).collect(Collectors.toList());
        roomAttrValueService.saveOrUpdateBatch(roomAttrValueList);

        //新增或更新房间相关配套
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        List<RoomFacility> roomFacilityList = facilityInfoIds.stream()
                .map(facilityInfoId -> { return RoomFacility.builder()
                .roomId(insertRoomId)
                .facilityId(facilityInfoId)
                .build();}).collect(Collectors.toList());
        roomFacilityService.saveOrUpdateBatch(roomFacilityList);

        //新增或更新房间相关标签
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        List<RoomLabel> roomLabelList = labelInfoIds.stream()
                .map(labelInfoId -> { return RoomLabel.builder()
                .roomId(insertRoomId)
                .labelId(labelInfoId)
                .build();}).collect(Collectors.toList());
        roomLabelService.saveOrUpdateBatch(roomLabelList);

        //新增或更新房间相关支付方式
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        List<RoomPaymentType> roomPaymentTypeList = paymentTypeIds.stream()
                .map(paymentTypeId -> { return RoomPaymentType.builder()
                .roomId(insertRoomId)
                .paymentTypeId(paymentTypeId)
                .build();}).collect(Collectors.toList());
        roomPaymentTypeService.saveOrUpdateBatch(roomPaymentTypeList);

        //新增或更新房间相关租期
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        List<RoomLeaseTerm> roomLeaseTermList = leaseTermIds.stream()
                .map(leaseTermId -> { return RoomLeaseTerm.builder()
                .roomId(insertRoomId)
                .leaseTermId(leaseTermId)
                .build();}).collect(Collectors.toList());
        roomLeaseTermService.saveOrUpdateBatch(roomLeaseTermList);

        //新增或更新房间相关图片
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfoList = graphVoList.stream()
                .map(graphVo -> {
                    GraphInfo graphInfo = new GraphInfo();
                    graphInfo.setItemId(insertRoomId);
                    graphInfo.setItemType(ItemType.ROOM);
                    graphInfo.setName(graphVo.getName());
                    graphInfo.setUrl(graphVo.getUrl());
                    return graphInfo;
                }).collect(Collectors.toList());
        graphInfoService.saveOrUpdateBatch(graphInfoList);

    }

    @Override
    @Transactional
    public void removeRoomInfoById(Long roomId) {
        //删除相关图片
        QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
        graphInfoQueryWrapper.eq("item_type", ItemType.ROOM);
        graphInfoQueryWrapper.eq("item_id", roomId);
        graphInfoService.remove(graphInfoQueryWrapper);

        //删除相关租期
        QueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new QueryWrapper<>();
        roomLeaseTermQueryWrapper.eq("room_id", roomId);
        roomLeaseTermService.remove(roomLeaseTermQueryWrapper);

        //删除相关支付方式
        QueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new QueryWrapper<>();
        roomPaymentTypeQueryWrapper.eq("room_id", roomId);
        roomPaymentTypeService.remove(roomPaymentTypeQueryWrapper);

        //删除相关房间标签
        QueryWrapper<RoomLabel> roomLabelQueryWrapper = new QueryWrapper<>();
        roomLabelQueryWrapper.eq("room_id", roomId);
        roomLabelService.remove(roomLabelQueryWrapper);

        //删除相关房间配套
        QueryWrapper<RoomFacility> roomFacilityQueryWrapper = new QueryWrapper<>();
        roomFacilityQueryWrapper.eq("room_id", roomId);
        roomFacilityService.remove(roomFacilityQueryWrapper);

        //删除相关房价属性
        QueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper = new QueryWrapper<>();
        roomAttrValueQueryWrapper.eq("room_id", roomId);
        roomAttrValueService.remove(roomAttrValueQueryWrapper);

        //删除房间基本属性
        this.removeById(roomId);
    }

    @Override
    public IPage<RoomItemVo> pageItem(IPage<RoomItemVo> itemVoIPage, RoomQueryVo queryVo) {
        IPage<RoomItemVo> iPage = baseMapper.pageItem(itemVoIPage, queryVo);
        return iPage;
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        // 房间数据
        RoomInfo roomInfo = baseMapper.selectById(id);

        // 公寓数据
//        QueryWrapper<ApartmentInfo> apartmentInfoQueryWrapper = new QueryWrapper<>();
//        apartmentInfoQueryWrapper.eq("id",roomInfo.getApartmentId());
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());

        // 图片
        QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
        graphInfoQueryWrapper.eq("item_type", ItemType.ROOM);
        graphInfoQueryWrapper.eq("item_id", id);
        List<GraphInfo> graphInfos = graphInfoMapper.selectList(graphInfoQueryWrapper);
        List<GraphVo> graphVos = graphInfos.stream().map(graphInfo -> {
            GraphVo graphVo = new GraphVo();
            BeanUtils.copyProperties(graphInfo, graphVo);
            return graphVo;
        }).collect(Collectors.toList());

        // 配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(id);

        // 标签
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);

        // 支付方式
        List<PaymentType> paymentTypes = paymentTypeMapper.selectListByRoomId(id);

        // 租期
        List<LeaseTerm> leaseTerms = leaseTermMapper.selectListByRoomId(id);

        // 基本属性
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectListByRoomId(id);


        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVos);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setLeaseTermList(leaseTerms);
        roomDetailVo.setPaymentTypeList(paymentTypes);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        return roomDetailVo;
    }
}




